package com.ms.p2p.controller.admin;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.bean.Message;
import com.ms.common.exception.NoticeException;
import com.ms.common.utils.MsDateUtils;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.entity.member.Auth;
import com.ms.p2p.entity.member.Member;
import com.ms.p2p.entity.system.AdminUser;
import com.ms.p2p.plugin.validation.annotation.RequiredStringValidator;
import com.ms.p2p.plugin.validation.annotation.Validations;
import com.ms.p2p.provider.AdminUserProvider;
import com.ms.p2p.provider.CertiFiCationProvider;
import com.ms.p2p.provider.MemberProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;


/**
 * 认证管理
 */
@Controller("AdminAuthController")
@RequestMapping("/admin/approve")
public class AdminAuthController extends AdminBaseController {

    @Reference
    private CertiFiCationProvider certificationProvider;
    @Reference
    private MemberProvider memberProvider;
    @Reference
    private AdminUserProvider empService;

    /**
     * 实名认证
     */
    @RequestMapping("/list")
    public String realApproves(String userId, String userName, String userRealName,
                               String begin_date, String end_date, String authStatus,
                               String currentPage, Model model) {

        return "admin/member/customer/approve/list";
    }
    @RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public PageBean queryData(String userId, String userName, String userRealName,
                              String begin_date, String end_date, String authStatus,

                              PageBean page
    ) {
        page.setList(certificationProvider.findPager(userId, userName, userRealName, begin_date, end_date, authStatus, page));
        page.setTotalCount(certificationProvider.findCount(userId, userName, userRealName, begin_date, end_date, authStatus));
        return JsonDataGridHelper.createJSONData(page);
    }


    @RequestMapping({"/{id}"})
    public String detail(@PathVariable String id, Model model) {
        if (StringUtils.isNotBlank(id)) {
            Auth realName = certificationProvider.findById(id);
            Member user = memberProvider.findById(realName.getUser_id());
            model.addAttribute("realName", realName);
            model.addAttribute("user", user);
            if (realName.getAudit_user() != null) {
                AdminUser emp = empService.findById(String.valueOf(realName.getAudit_user()));
                model.addAttribute("emp", emp);
            }
        }
        return "admin/member/customer/approve/input";
    }

    /**
     * 实名认证审核
     */
    @RequestMapping("/save")
    @ResponseBody
    @Validations(requiredStrings = {
            @RequiredStringValidator(fieldName = "remark", message = "备注信息不能为空")
    })
    public Message save(String remark, Integer status, String id)
            throws IllegalAccessException, InvocationTargetException,
            IntrospectionException {
        AdminUser emp = getCurrentEmp();
        if (StringUtils.isNotBlank(id)) {
            Auth realName = certificationProvider.findById(id);
            realName.setStatus(status);
            realName.setAudit_user(emp.getId());
            realName.setAudit_time(MsDateUtils.getTime());
            realName.setRemark(remark);
            certificationProvider.update(realName);
            return Message.success("审核成功");
        }
       throw  new NoticeException("审核失败");
    }

    @Override
    public boolean getBinderFlag() {
        return true;
    }
}
